import user.User;
import utils.JdbcUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.time.LocalDateTime;

public class Blog_windows{
    //创建窗体对象并命名
    JFrame frame = new JFrame("Blog_windows");
    public void init() throws Exception {
        //设置窗体关闭模式
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗体大小
        frame.setSize(500, 300);
        //设置窗体居中
        frame.setLocationRelativeTo(null);
        //取消布局
        frame.setLayout(null);
        //设置窗口不能改变大小
        frame.setResizable(false);
        //设置文章标题
        Box titlebox=Box.createHorizontalBox();
        JLabel title = new JLabel("欢迎使用博客后台管理系统");
        Font font=new Font("宋体",Font.BOLD,30);
        title.setFont(font);
        titlebox.add(title);
        //定位标题
        titlebox.setBounds(60,35,400,40);
        frame.add(titlebox);
        //组装用户名
        Box uBox=Box.createHorizontalBox();
        JLabel uLabel=new JLabel("用户名:");
        JTextField uField = new JTextField(15);
        uBox.add(uLabel);
        uBox.add(Box.createHorizontalStrut(20));
        uBox.add(uField);
        //定位用户名
        uBox.setBounds(140,100,200,20);
        frame.add(uBox);
        //组装密码
        Box pBox=Box.createHorizontalBox();
        JLabel pLabel=new JLabel("密    码:");
        JPasswordField pField = new JPasswordField(15);
        pField.setEchoChar('*');
        pBox.add(pLabel);
        pBox.add(Box.createHorizontalStrut(20));
        pBox.add(pField);
        //定位密码
        pBox.setBounds(140,140,200,20);
        frame.add(pBox);
        //组装按钮
        Box btnbox = Box.createHorizontalBox();
        JButton loginBtn =new JButton("登录");
        JButton rigestBtn=new JButton("注册");
        btnbox.add(loginBtn);
        btnbox.add(Box.createHorizontalStrut(60));
        btnbox.add(rigestBtn);
        //定位按钮
        btnbox.setBounds(150,200,200,20);
        frame.add(btnbox);
        //登录按钮交互
        loginBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户输入的数据
                String username = uField.getText().trim();
                String password = pField.getText().trim();
                //访问登录
                boolean YN = User.login(username,password);
                if(YN){
                    LocalDateTime time=LocalDateTime.now();
                    JdbcUtils.JdbcUpdate(User.sql_updata_lasttime,time,username);
                    JOptionPane.showMessageDialog(frame,"登录成功!");
                    try {
                        new Main_page().init(username);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    frame.dispose();
                }else{
                    JOptionPane.showMessageDialog(frame,"登录失败!");
                }
            }
        });
        //注册按钮交互
        rigestBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String key=JOptionPane.showInputDialog(frame,"请输入注册密钥:");
                boolean key_yn= User.key_detection(key);
                int power =(key.equals(User.user_key_f)?1:2);
                if (key_yn){
                    new RegisterInerface().init(power);
                    //当前页面消失
                    frame.dispose();
                }else{
                    JOptionPane.showMessageDialog(frame,"密钥输入错误");
                }
            }
        });
        //设置窗体是否可见
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        try {
            new Blog_windows().init();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}